/**
 * 2013 2013-5-8
 */
package com.surfilter.dp.dao;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;

import com.surfilter.dp.model.ActionModel;

/**
 *
 */
public class ActionDao extends BaseDao {
	
	public List<ActionModel> getActionByModuleId(int id){
		PreparedStatement pstmt = this.getPstmt("select * from PL_ACTION where MODULE_ID=? order by ID asc");
		List<ActionModel> retList = new ArrayList<ActionModel>();
		try {
			pstmt.setInt(1, id);
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				ActionModel action = new ActionModel();
				action.setActionName(rs.getString("ACTION_NAME"));
				action.setActionText(rs.getString("ACTION_TEXT"));
				action.setModuleId(id);
				action.setId(rs.getInt("ID"));
				retList.add(action);
			}
			rs.close();
			this.closePstmt(pstmt);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return retList;
	}

	/**
	 * @param actions
	 */
	public void batchUpdateActions(List<ActionModel> actions) {
		PreparedStatement pstmt = this.getPstmt("update pl_action set ACTION_NAME=?,ACTION_TEXT=?,UPDATE_TIME=? where id=?");
		for (ActionModel action : actions) {
			try {
				pstmt.setString(1, action.getActionName());
				pstmt.setString(2, action.getActionText());
				pstmt.setDate(3, new Date(System.currentTimeMillis()));
				pstmt.setInt(4, action.getId());
				pstmt.addBatch();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		try {
			pstmt.executeBatch();
			this.closePstmt(pstmt);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

	/**
	 * @param action
	 */
	public void addNewAction(ActionModel action) {
		PreparedStatement pstmt = this.getPstmt("insert into pl_action(ACTION_NAME,ACTION_TEXT,MODULE_ID,CREATE_TIME) values(?,?,?,?)");
		try {
			pstmt.setString(1, action.getActionName());
			pstmt.setString(2, action.getActionText());
			pstmt.setInt(3, action.getModuleId());
			pstmt.setDate(4, new Date(System.currentTimeMillis()));
			pstmt.execute();
			this.closePstmt(pstmt);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param actions
	 */
	public void deleteActions(List<ActionModel> actions) {
		PreparedStatement pstmt = this.getPstmt("delete from pl_action where ID=?");
		for (ActionModel actionModel : actions) {
			try {
				pstmt.setInt(1, actionModel.getId());
				pstmt.addBatch();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		try {
			pstmt.executeBatch();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		this.closePstmt(pstmt);
	} 
}
